/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package assignment_cp3120.manager.createflights;

import assignment_cp3120.DAO.Factory.DAOFactory;
import assignment_cp3120.dataObjects.AllFlights;
import assignment_cp3120.dataObjects.AllLocations;
import assignment_cp3120.dataObjects.Flight;
import java.sql.Date;
import java.sql.Time;
import java.util.Vector;

/**
 *
 * @author Administrator
 */
public class CreateFlightsModel {
    private Flight newFlight;
    private final String DEFAULT_CODE = "JC00001";

    public CreateFlightsModel() {
        newFlight = new Flight();
    }        
    public Flight getNewFlight() {
        return newFlight;
    }
    public void setNewFlight(Flight newFlight) {
        this.newFlight = newFlight;
    }
    public void generateFlightID(){
        String txtCode = "";
        String numberTxt = "";
        int numberCode = 0;
        char tempChar = '!';
        
        if (AllFlights.getInstance().size() > 0) {
            for (int i = 0; i < AllFlights.getInstance().elementAt(0).getFlight_id().length(); i++) {
                tempChar = AllFlights.getInstance().elementAt(0).getFlight_id().charAt(i);
                if (Character.isDigit(tempChar) != true){
                    txtCode += tempChar;
                }
            }
            for (Flight flight : AllFlights.getInstance()) {
                try {
                    if(numberCode < Integer.parseInt(flight.getFlight_id().substring(txtCode.length()))){
                        numberCode = Integer.parseInt(flight.getFlight_id().substring(txtCode.length()));
                    }
                } catch (NumberFormatException e){
                    System.out.println("WARNING: A non-valid flight ID found in database");
                }
            }
            //this flight id is one more then previous
            numberCode++;
            
            numberTxt = Integer.toString(numberCode);
            
            for (int i = 0; i < DEFAULT_CODE.length()-txtCode.length(); i++) {
                if (numberTxt.length()+txtCode.length() < DEFAULT_CODE.length()) {
                    numberTxt = "0" + numberTxt;
                }
            }
            txtCode += numberTxt;
            newFlight.setFlight_id(txtCode);
        }
        else {
            newFlight.setFlight_id(DEFAULT_CODE);
        }
    }
    public void setFlightID (String id) {
        newFlight.setFlight_id(id);
    }
    public void setDepID(String dep_id){
        newFlight.setDep_id(dep_id);
    }
    public void setDestID(String dest_id) {
        newFlight.setDest_id(dest_id);
    }
    public void setDate(Date date) {
         newFlight.setFlight_date(date);
    }
    public void setArrTime(Time arr_time) {
         newFlight.setArr_time(arr_time);
    }
    public void SetDepTime(Time dep_time) {
         newFlight.setDep_time(dep_time);
    }
    public void setFlightCapacity (int capacity) {
        newFlight.setCapacity(capacity);
    }
    public void setFlightPrice(int prices){
         newFlight.setFlight_price(prices);
    }    
    public Vector<Flight> getFlights() {
        return AllFlights.getInstance();
    }
    public void addFlight() {
        AllFlights.add(newFlight);
    }
}
